package com.utcs.modules.resource.basic.service;

import com.utcs.common.persistence.Page;
import com.utcs.common.service.TreeService;
import com.utcs.modules.resource.basic.dao.DepartmentDao;
import com.utcs.modules.resource.basic.entity.Department;
import com.utcs.modules.resource.basic.entity.User;
import com.utcs.modules.resource.basic.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fly on 2017/5/23.
 * 部门Serivce
 */
@Service
@Transactional(readOnly = true)
public class DepartmentService extends TreeService<DepartmentDao,Department>{

    @Autowired
    DepartmentDao departmentDao;

    /**
     * 分页查询部门信息
     * @param page
     * @param department
     * @return
     */
    public Page<Department> findList(Page<Department> page, Department department){
        List<Department> departmentList;
        User user = UserUtils.getUser();
        department.setPage(page);
        if(user.isAdmin()){
            departmentList = departmentDao.findAllList(department);
        }else {
            departmentList = departmentDao.findList(department);
        }
        page.setList(departmentList);
        return page;
    }
    /**
     * 获取所有部门列表
     * @return
     */
    public List<Department> findAll(){
        return UserUtils.getDepartmentAllList();
    }


    @Transactional(readOnly = false)
    public void save(Department department) {
        super.save(department);
        UserUtils.removeCache(UserUtils.CACHE_DEPARTMENT_LIST);
    }

    @Transactional(readOnly = false)
    public void delete(Department department) {
        super.delete(department);
        UserUtils.removeCache(UserUtils.CACHE_DEPARTMENT_LIST);
    }
}
